<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { gamesApi } from '@/api/controller';
  import { ToastSuccess, Toastloading, allNumber } from '@/utils/public';
  const formLabelWidth = '120px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    stateList:[] as Array<any>
  })
  interface formQuert {
    rain_id:number
    start_time:string
    recharge_diamond_num:number | string
    all_diamond_num:number | string
    packet_num:number | string
    min_num:number | string
    max_num:number | string
  }
  let form = reactive({} as formQuert)
  const formRules = {
    start_time: [{ required: true, trigger: 'blur', message: '请选择开始时间' }],
    recharge_diamond_num: [{ required: true, trigger: 'blur', message: '请输入充值钻石数' }],
    all_diamond_num: [{ required: true, trigger: 'blur', message: '请输入总钻石数' }],
    packet_num: [{ required: true, trigger: 'blur', message: '请输入总个数' }],
    min_num: [{ required: true, trigger: 'blur', message: '请输入每个最小值' }],
    max_num: [{ required: true, trigger: 'blur', message: '请输入每个最大值' }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      let submitSet = false
      const { rain_id } = form
      if(rain_id) {
        await gamesApi.redPacketRainEdit(form).then(()=>{
          ToastSuccess('编辑成功')
          submitSet = true
        }).catch(()=>{
          loading.close()
        })
      }
      else {
        await gamesApi.redPacketRainAdd(form).then(()=>{
          ToastSuccess('新增成功')
          submitSet = true
        }).catch(()=>{
          loading.close()
        })
      }
      if(submitSet) {
        loading.close()
        $emit('updateList')
        closeDialog()
      }
    })
  }
  const openDialog = (query:any = {})=> {
    const {
      id:rain_id, start_time, recharge_diamond_num,
      all_diamond_num, packet_num, min_num, max_num,
    } = query
    const object:any = {
      rain_id, start_time, recharge_diamond_num,
      all_diamond_num, packet_num, min_num, max_num,
    }
    if(rain_id) object['rain_id'] = rain_id
    Object.assign(form,object)
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="5vh"
    class="xm-dialog input-width"
    v-model="action.dialogFormVisible"
    :close-on-click-modal="action.disabled"
    :show-close="action.disabled"
    :close-on-press-escape="action.disabled"
    width="60%"
    title="一起玩列表新增">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="开始时间" prop="start_time">
          <el-date-picker
          v-model="form.start_time"
          type="datetime"
          format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择开始时间" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="充值钻石数" prop="recharge_diamond_num">
          <el-input
            v-model="form.recharge_diamond_num"
            @input="allNumber(form,'recharge_diamond_num')"
            placeholder="请输入充值钻石数" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="总钻石数" prop="all_diamond_num">
          <el-input
            v-model="form.all_diamond_num"
            @input="allNumber(form,'all_diamond_num')"
            placeholder="请输入总钻石数" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="总个数" prop="packet_num">
          <el-input
            v-model="form.packet_num"
            @input="allNumber(form,'packet_num')"
            placeholder="请输入总个数" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="每个最小值" prop="min_num">
          <el-input
            v-model="form.min_num"
            @input="allNumber(form,'min_num')"
            placeholder="请输入每个最小值" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="每个最大值" prop="max_num">
          <el-input
            v-model="form.max_num"
            @input="allNumber(form,'max_num')"
            placeholder="请输入每个最大值" />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>